/*
 * Copyright © 2021 Dowsure
 * https://www.dowsure.com/
 *
 * All rights reserved.
 */

package com.dowsure.apisaas.api;

import com.dowsure.apisaas.enums.CreditStatusEnums;
import com.dowsure.apisaas.enums.MethodEnums;
import com.dowsure.apisaas.exception.ApiException;
import com.dowsure.apisaas.model.CreditCloseRequestParams;
import com.dowsure.apisaas.model.CreditLoanRequestParams;
import com.dowsure.apisaas.model.CreditResultRequestParams;
import com.dowsure.apisaas.model.SellerShopDataRequestParams;
import com.dowsure.apisaas.util.ApiClient;
import com.dowsure.apisaas.util.ApiResponse;
import com.dowsure.apisaas.util.RequestParamValidatedUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @description: 授信
 * @author: Dowsure
 * @create: 2021-12-14 14:06
 **/
public class CreditApi {

    private ApiClient apiClient;

    public CreditApi(ApiClient apiClient){
        this.apiClient = apiClient;
    }


    /**
     * 8、传输授信结果（API-C07）
     * 用户对卖家授信后需要传递授信结果给Dowsusre，Dowsure接收到消息之后通知平台授信结果：
     * 若授信通过，卖家可自行去Amazon卖家中心锁定账户。
     * @author Dowsure
     * @param creditResultRequestParams {@link SellerShopDataRequestParams}
     * @return {@link ApiResponse <String>}
     * @throws ApiException
     */
    public ApiResponse<String> result(CreditResultRequestParams creditResultRequestParams) throws ApiException {
        RequestParamValidatedUtils.validated(creditResultRequestParams);
        if (CreditStatusEnums.APPROVE.equals(creditResultRequestParams.getCreditStatus())){
            RequestParamValidatedUtils.notBlank(creditResultRequestParams.getStartTime(), "startTime（授信开始时间）");
            RequestParamValidatedUtils.notBlank(creditResultRequestParams.getEndTime(), "endTime（授信结束时间）");
            RequestParamValidatedUtils.notBlank(creditResultRequestParams.getTerm(), "term（授信期限长度）");
            RequestParamValidatedUtils.notBlank(creditResultRequestParams.getTermUnit(), "termUnit（授信期限单位）");
            RequestParamValidatedUtils.notBlank(creditResultRequestParams.getApr(), "apr（授信年利率）");
            RequestParamValidatedUtils.notBlank(creditResultRequestParams.getCreditCode(), "creditCode（授信编码）");
            RequestParamValidatedUtils.notBlank(creditResultRequestParams.getAmount(), "amount（授信金额）");
            RequestParamValidatedUtils.notBlank(creditResultRequestParams.getCurrency(), "currency（币种）");
        } else if (CreditStatusEnums.REJECT.equals(creditResultRequestParams.getCreditStatus())){
            RequestParamValidatedUtils.notBlank(creditResultRequestParams.getReason(), "reason（拒绝原因）");
        }
        String localVarPath = "/v1/credit/result";

        Map<String, String> headerParams = new HashMap<>();

        com.squareup.okhttp.Call call = apiClient.buildCall(localVarPath, MethodEnums.POST, creditResultRequestParams, headerParams);
        return apiClient.execute(call);
    }

    /**
     * 9、传输贷款支用数据（API-C08）
     * 用户对卖家授信后，卖家发起支用并且支用完毕后，需要传递支用数据给Dowsure。该支用数据需要在卖家中心展示给卖家。
     *    a.需要提供卖家在Dowsure 的申请单编号。
     *    b.需要提供卖家的支用数据。
     * 注：卖家支用前是否需要锁定账户，由用户侧提供，默认锁定。
     * @author Dowsure
     * @param creditLoanRequestParams {@link CreditLoanRequestParams}
     * @return {@link ApiResponse <String>}
     * @throws ApiException
     */
    public ApiResponse<String> loan(CreditLoanRequestParams creditLoanRequestParams) throws ApiException {
        RequestParamValidatedUtils.validated(creditLoanRequestParams);
        String localVarPath = "/v1/credit/loan";

        Map<String, String> headerParams = new HashMap<>();
        com.squareup.okhttp.Call call = apiClient.buildCall(localVarPath, MethodEnums.POST, creditLoanRequestParams, headerParams);
        return apiClient.execute(call);
    }

    /**
     * 12、传输授信关闭请求（API-C11）
     * 当卖家授信到期或者提前关闭授信，用户需要调用此接口完成授信关闭操作
     * 注：授信一旦关闭，卖家平台账户自动解锁。
     * @author Dowsure
     * @param creditCloseRequestParams {@link CreditCloseRequestParams}
     * @return {@link ApiResponse <String>}
     * @throws ApiException
     */

    public ApiResponse<String> close(CreditCloseRequestParams creditCloseRequestParams) throws ApiException {
        RequestParamValidatedUtils.validated(creditCloseRequestParams);
        String localVarPath = "/v1/credit/close";

        Map<String, String> headerParams = new HashMap<>();
        com.squareup.okhttp.Call call = apiClient.buildCall(localVarPath, MethodEnums.POST, creditCloseRequestParams, headerParams);
        return apiClient.execute(call);
    }
}
